<script>
    function goBack() {
	window.location.href = "?modul=products&action=products_list";
    }
</script>
<?php

function V_Add_Products($arrayData, $mode) {
    $ulr = "?" . MODUL . "=products&" . ACTION . "=" . $mode;
    if ($mode == 'Edit_product') {
	$strLable = "Cập nhật sản phẩm";
	$strButton = "Cập nhật";
    } else if ($mode == 'add_products') {
	$strLable = "Thêm mới sản phẩm";
	$strButton = "Thêm mới";
    }
    ?>
    <h3><?= $strLable ?></h3>
    <form enctype="multipart/form-data" action="<?= $ulr ?>" method="post" name="frm" onSubmit="return validateForm();">
        <input type="hidden" name="id" id="id" value="<?= $arrayData['id']; ?>"/>
        <table width="950px" cellpadding="5" border="1">
    	<tr style="background-color: #99CCFF">
    	    <th colspan="2">Thông tin sản phẩm</th>
    	</tr>
    	<tr>
    	    <td width="200px"><strong>Chọn nhóm</strong></td>
    	    <td>
		<select name="product_type_id">
    		    <option value="0">---ROOT---</option>
			<? products_type(0, 0, $arrayData['product_type_id']); ?>
    		</select>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Tên sản phẩm</strong></td>
    	    <td>
    		<input type="text" name="product_name" id="product_name" value="<?= $arrayData['product_name']; ?>"/>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Ảnh</strong></td>
    	    <td>
		    <?
		    if ($mode == "Edit_product") {
			if ($arrayData['product_img'] != "") {
			    ?>
	    		<img width="250px" src="<?= DIR_IMG_PRODUCT . $arrayData['product_img'] ?>"/><br/><br/>
			    <?
			    echo $arrayData['product_img']."<br/>";
			}
		    }
		    ?>
    		<input type="file" name="product_img" id="product_img" value="<?= $arrayData['product_img']; ?>"/>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Giá</strong></td>
    	    <td>
    		<input type="text" name="product_price" id="product_price" value="<?= $arrayData['product_price']; ?>"/>
    	    </td>
    	</tr>
    	<tr>
    	    <td><strong>Ghi chú</strong></td>
    	    <td>
    		<textarea style="width: 400px; height: 150px" name="product_note" id="product_note"><?= $arrayData['product_note']; ?></textarea>
    	    </td>
    	</tr>
    	<tr>
    	    <td colspan="2">
    		<div style="float: right">
    		    <input type="reset" name="reset"  value="Nhập lại"/>
    		    <input type="submit" name="Submit" value="<?= $strButton ?>"/>
    		    <input type="button" name="Back" value="Quay lại" onclick="goBack();"/>
    		</div>
    	    </td>
    	</tr>
        </table>
    </form>
    <?
}

function products_type($parent_id, $level, $id_nhom) {
    global $db;
    $objProducts = new MD_Products($db);
    $where = " type_parent = '$parent_id'";
    $order = "";
    $limit = "";
    $array = $objProducts->get_product_type_by_Where($where, $order, $limit);
    foreach ($array as $value) {
	$id = $value['id'];
	$selected = "";
	if ($id_nhom == $id) {
	    $selected = "Selected";
	}
	echo '<option ' . $selected . ' value="' . $id . '">' . str_repeat('- - ', $level) . $value['type_name'] . '</option>';
	products_type($id, $level + 1, $id_nhom);
    }
}
?>
